package com.cskaoyan.service.order;

import com.cskaoyan.bean.MallOrder;
import com.cskaoyan.bean.MallOrderExample;
import com.cskaoyan.bean.MallOrderGoods;
import com.cskaoyan.bean.MallOrderGoodsExample;

import com.cskaoyan.mapper.MallOrderGoodsMapper;
import com.cskaoyan.mapper.MallOrderMapper;
import com.cskaoyan.mapper.MallUserMapper;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.order.OrderListBo;
import com.cskaoyan.model.bo.order.OrderShipBo;
import com.cskaoyan.model.vo.BaseData;
import com.cskaoyan.model.vo.order.OrderChannelVo;
import com.cskaoyan.model.vo.order.OrderDetailVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.javassist.expr.Cast;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: mjm
 * @Date: 2021/11/30
 * @Time: 16:57
 * @Description:
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    MallOrderGoodsMapper mallOrderGoodsMapper;

    @Autowired
    MallOrderMapper mallOrderMapper;

    @Autowired
    MallUserMapper mallUserMapper;

    @Override
    public BaseData orderList(OrderListBo orderListBo, PageBo param) {
        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());
        MallOrderExample example = new MallOrderExample();
        example.setOrderByClause(param.getSort() + " " + param.getOrder());

        MallOrderExample.Criteria criteria = example.createCriteria();


        ArrayList<Short> orderStatusArray = orderListBo.getOrderStatusArray();
        if (orderStatusArray != null && !"".equals(orderStatusArray)) {
            criteria.andOrderStatusIn(orderStatusArray);
        }

        String orderId = orderListBo.getOrderId();
        if (orderId != null && !"".equals(orderId)) {
            criteria.andIdEqualTo(Integer.parseInt(orderId));
        }

        Date start = orderListBo.getStart();
        if (start != null && !"".equals(start)) {
            criteria.andAddTimeGreaterThanOrEqualTo(start);
        }

        Date end = orderListBo.getEnd();
        if (end != null && !"".equals(end)) {
            criteria.andAddTimeLessThanOrEqualTo(end);
        }

        String userId = orderListBo.getUserId();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }

        String orderSn = orderListBo.getOrderSn();
        if (orderSn != null && !"".equals(orderSn)) {
            criteria.andOrderSnLike("%" + orderSn + "%");
        }
        //逻辑删除的页面不显示
        criteria.andDeletedEqualTo(false);


        List<MallOrder> mallOrder = mallOrderMapper.selectByExample(example);

        PageInfo info = new PageInfo(mallOrder);
        long total = info.getTotal();
        return BaseData.list(mallOrder, total);
    }

    @Override
    public OrderDetailVo orderDetail(Integer id) {
        OrderDetailVo orderDetailVo = mallOrderMapper.orderDetail(id);
        orderDetailVo.setUser(mallOrderMapper.selectUser(orderDetailVo.getOrder().getUserId()));

        return orderDetailVo;
    }


    @Override
    public int orderRefund(Integer orderId, BigDecimal refundMoney) {


        MallOrder mallOrder = new MallOrder();
        mallOrder.setId(orderId);
        mallOrder.setRefundAmount(refundMoney);
        mallOrder.setRefundTime(new Date());
        mallOrder.setUpdateTime(new Date());
        mallOrder.setOrderStatus((short) 203);
        int affectedRows = mallOrderMapper.updateByPrimaryKeySelective(mallOrder);
        return affectedRows;
    }

    @Override
    public ArrayList<OrderChannelVo> orderChannel() {
        ArrayList<OrderChannelVo> orderChannelVos = null;
        return orderChannelVos;
    }

    @Override
    public int orderShip(OrderShipBo orderShipBo) {

        MallOrder mallOrder = new MallOrder();
        mallOrder.setId(orderShipBo.getOrderId());
        mallOrder.setShipSn(orderShipBo.getShipSn());
        mallOrder.setShipChannel(orderShipBo.getShipChannel());
        mallOrder.setShipTime(new Date());
        mallOrder.setUpdateTime(new Date());
        mallOrder.setOrderStatus((short) 301);
        int affectedRows = mallOrderMapper.updateByPrimaryKeySelective(mallOrder);
        return affectedRows;

    }
}
